﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using System.Collections;

namespace TP2
{
    public partial class AccumulationOperationForm : Form
    {
       
        AccumulationForm accumulationForm;
        string client_type;
        int id_client;
        int deposit;
        int proc;
        bool flag;
        int number;
        public AccumulationOperationForm(string clientType, AccumulationForm Fr, int idClient,int deposit,int procent, int Number)
        {
            id_client = idClient;
            client_type = clientType;
            this.number = Number;
            this.proc = procent;
            this.deposit = deposit;
            accumulationForm = Fr;
            InitializeComponent();
        }

        private void SelectFromAccumulation(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selectRate = new ClassRateType();
            selectRate = dbWork.SelectFromAccumulationRate(client_type);
            int j = 0;
            if (selectRate.id_rate.Count == 0) 
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";

            }
            else { dataGridView1.RowCount = selectRate.id_rate.Count; }
            while (j < selectRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selectRate.title[j].ToString());
                dataGridView1[1, j].Value = selectRate.procent[j].ToString();
                dataGridView1[2, j].Value = selectRate.period[j].ToString();
                j++;
            }          
        }

        private void AccumulationOperationForm_Load(object sender, EventArgs e)
        {
            this.SelectFromAccumulation(client_type);
            labelDeposit.Text = deposit.ToString();
            labelprocent.Text = proc.ToString();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                deposit = deposit + Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text);
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.UpdateAccumulation(deposit, flag, period, dateTimePicker1.Value, number, procent);
                accumulationForm.RefreshTable();
                this.Close();
            }
            catch (FormatException) { MessageBox.Show("ошибка во входных данных"); }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                flag = true;
                dateTimePicker1.Enabled = true;
                dataGridView1.Enabled = true;
            }
            else
            {
                flag = false;
                dateTimePicker1.Enabled = false;
                dataGridView1.Enabled = false;
            }
            
        }
    }
}
